#include "pch.h"
#include "DisplayEngine.h"
#include "DisplayContext.h"
#include "RectangleDisplayItem.h"
#include "ImageDisplayItem.h"
#include "DisplayItem.h"
using namespace D2D1;
using namespace DirectX;
using namespace Microsoft::WRL;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Core;

DisplayEngine::DisplayEngine()
{
	
}

DisplayEngine::~DisplayEngine()
{
	for(int i=0;i<m_displayItems.size();++i)
	{
		delete m_displayItems.at(i);
	}
	m_displayItems.clear();
}

void DisplayEngine::InitializeWithFrame(float width,float height)
{

}

void DisplayEngine::AppendDisplayItem(DisplayItem* item)
{
	m_displayItems.push_back(item);
	Render();
}

void DisplayEngine::AddImage(float x,float y,float width,float height,Windows::Storage::StorageFile^ file)
{
	ImageDisplayItem* item=new ImageDisplayItem();
	item->X=x;
	item->Y=y;
	item->Width=width;
	item->Height=height;
	item->Create(Context);
	item->SetSource(file);
	m_displayItems.push_back(item);
}

void DisplayEngine::AddRect(float x,float y,float width,float height,UINT32 borderColor,UINT32 fillColor,FLOAT borderThickness)
{
	RectangleDisplayItem* item=new RectangleDisplayItem();
	item->X=x;
	item->Y=y;
	item->Width=width;
	item->Height=height;
	item->BorderColor=borderColor;
	item->FillColor=fillColor;
	item->BorderThickness=borderThickness;
	item->Create(Context);
	m_displayItems.push_back(item);
}

void DisplayEngine::Initialize(Windows::UI::Core::CoreWindow^ window, Windows::UI::Xaml::Controls::SwapChainBackgroundPanel^ panel, float dpi)
{
	DirectXBase::Initialize(window,panel,dpi);
	Context.D2DContext=m_d2dContext;
	Context.WICFactory=m_wicFactory;
}

void DisplayEngine::CreateDeviceIndependentResources()
{
	DirectXBase::CreateDeviceIndependentResources();
}

void DisplayEngine::CreateDeviceResources()
{
	DirectXBase::CreateDeviceResources();
}

void DisplayEngine::CreateWindowSizeDependentResources()
{
	DirectXBase::CreateWindowSizeDependentResources();

	// Add code to create window size dependent objects here.
}

void DisplayEngine::Render()
{
	m_d2dContext->BeginDraw();
	m_d2dContext->Clear();
	//// Position the rendered text.
	//Matrix3x2F translation = Matrix3x2F::Translation(
	//	m_windowBounds.Width / 2.0f - m_textMetrics.widthIncludingTrailingWhitespace / 2.0f + m_textPosition.X,
	//	m_windowBounds.Height / 2.0f - m_textMetrics.height / 2.0f + m_textPosition.Y
	//	);

	//// Note that the m_orientationTransform2D matrix is post-multiplied here
	//// in order to correctly orient the text to match the display orientation.
	//// This post-multiplication step is required for any draw calls that are
	//// made to the swap chain's target bitmap. For draw calls to other targets,
	//// this transform should not be applied.
	//m_d2dContext->SetTransform(translation * m_orientationTransform2D);

	for(int i=0;i<m_displayItems.size();++i)
	{
		m_displayItems.at(i)->Render(Context);
	}

	// Ignore D2DERR_RECREATE_TARGET. This error indicates that the device
	// is lost. It will be handled during the next call to Present.
	HRESULT hr = m_d2dContext->EndDraw();
	if (hr != D2DERR_RECREATE_TARGET)
	{
		DX::ThrowIfFailed(hr);
	}
}